# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

  helper :all # include all helpers, all the time
  protect_from_forgery # See ActionController::RequestForgeryProtection for details
 
  def iphone_test_mode?
    @@iphone_test_mode
  end


  def force_iphone_format
    request.format = :iphone
  end

  def set_iphone_format
    if is_iphone_request? || is_iphone_format? || is_iphone_subdomain?
      request.format = if cookies["browser"] == "desktop"
        then :html
      else :iphone
      end
    end
  end

  def is_iphone_format?
    request.format.to_sym == :iphone
  end

  def is_iphone_request?
    request.user_agent =~ /(Mobile\/.+Safari)|iPhone|android/
  end

  def is_iphone_subdomain?
    request.subdomains.first == "iphone"
  end

  def ApplicationController.acts_as_iphone_controller(iphone_test_mode = nil)
    if iphone_test_mode
      before_filter :force_iphone_format
    else
      before_filter :set_iphone_format
    end
    helper_method :is_iphone_request?

  end
  acts_as_iphone_controller()
  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  
  def index
    @records = @model.first(20)
    @empty=@model.new
    render :partial=>"/index" ,:content_type => 'text/html'
  end
  # GET /books/1
  # GET /books/1.xml
  def show
    @record = @model.find(params[:id])
    render :partial=>"/show" ,:content_type => 'text/html'
  end

  # GET /books/new
  # GET /books/new.xml
  def new
    @record = @model.new
    render :partial=>"/new" ,:content_type => 'text/html'
  end

  # GET /books/1/edit
  def edit
    @record = @model.find(params[:id])
    @re
    render :partial=>"/edit" ,:content_type => 'text/html'
  end

  # POST /books
  # POST /books.xml
  def create
    @record = @model.new(params[@name])
    if @record.save
      flash[:notice] = 'Media was successfully created.'
      index
    else
      render :partial=>"/new", :status => 303,:content_type => 'text/html'
    end
  end

  # PUT /books/1
  # PUT /books/1.xml
  def update
    @record = @model.find(params[:id])
    if @record.update_attributes(params[@name])
      flash[:notice] = 'Media was successfully updated.'
      render :partial=>"/show"
    else
      render :partial=>"/edit"
    end
  end

  # DELETE /books/1
  # DELETE /books/1.xml
  def destroy
    @record = @model.find(params[:id])
    @record.destroy
    
    redirect_to(@record)
  end
  def search
      @records = @model.find_by_sql("name like '%s%'")
      print "\n records\n"
      print @records
      print "\n\n\n"
      render :partial=>"/search" ,:content_type => 'text/html'
  end
end

#if request.xhr?
